# Cool tutorials
# https://blog.codecentric.de/en/2012/06/the-listener-interface-of-the-robot-framework/

# Display modules: http://excess.org/urwid/docs/manual/displaymodules.html

import urwid
from robot_screen import RobotScreen


class _AbstractTextDialog:

    def __init__(self):
        self._result = None
        self._pallet = [
            ('banner', '', '', '', '#ffa', '#60d'),
            ('streak', '', '', '', 'g50', '#60a'),
            ('inside', '', '', '', 'g38', '#808'),
            ('outside', '', '', '', 'g27', '#a06'),
            ('bg', '', '', '', 'g7', '#d06'),]


        self._placeholder = urwid.SolidFill()
    
    
    def exit_on_q(self, key):
        if key in ('q', 'Q'):
            raise urwid.ExitMainLoop()        



class MessageDialog(_AbstractTextDialog):

    def __init__(self, message):
        _AbstractTextDialog.__init__(self)
        self._message = message;
        
    def show(self):

        plt = [
            ('banner', '', '', '', '#ffa', '#60d'),
            ('streak', '', '', '', 'g50', '#60a'),
            ('inside', '', '', '', 'g38', '#808'),
            ('outside', '', '', '', 'g27', '#a06'),
            ('bg', '', '', '', 'g7', '#d06'),]
        
        placeholder = urwid.SolidFill()


        loop = urwid.MainLoop(self, palette=plt, screen=RobotScreen(), unhandled_input=self.exit_on_q)
        loop.screen.set_terminal_properties(colors=256)
        loop.widget = urwid.AttrMap(placeholder, 'bg')
        loop.widget.original_widget = urwid.Filler(urwid.Pile([]))

        div = urwid.Divider()
        outside = urwid.AttrMap(div, 'outside')
        inside = urwid.AttrMap(div, 'inside')
        txt = urwid.Text(('banner', self._message), align='center')
        streak = urwid.AttrMap(txt, 'streak')
        pile = loop.widget.base_widget # .base_widget skips the decorations
        for item in [outside, inside, streak, inside, outside]:
            pile.contents.append((item, pile.options()))
        
        loop.run()        
        
        
        
        
        
# Works
class InputDialog(_AbstractTextDialog):

    def __init__(self, message, default):
        
        _AbstractTextDialog.__init__(self)
        self._message = message
        self._result = default
         
    class _QuestionBox(urwid.Filler):
        
        def __init__(self, edit):
            self._edit = edit
            urwid.Filler.__init__(self, edit)
        
        def keypress(self, size, key):
            if key != 'enter':
                return super(InputDialog._QuestionBox, self).keypress(size, key)
           
            raise urwid.ExitMainLoop()
        
        
    def show(self):

#         self._edit = urwid.Edit([self._message, u"[", self._result, u"]\n"])
        self._edit = urwid.Edit([self._message, u"\n"])
        fill = self._QuestionBox(self._edit);
        urwid.MainLoop(fill, screen=RobotScreen(), unhandled_input=self.exit_on_q).run()
        
        if self._edit.edit_text:
            self._result = self._edit.edit_text       
        
        return self._result

# Works
class SelectionDialog(_AbstractTextDialog):

    def __init__(self, message, options):
        self._message = message
        self._options = options
        _AbstractTextDialog.__init__(self)
        
        
        
    def menu(self, title, choices):
        body = [urwid.Text(title), urwid.Divider()]
        for c in choices:
            button = urwid.Button(c)
            urwid.connect_signal(button, 'click', self.item_chosen, c)
            body.append(urwid.AttrMap(button, None, focus_map='reversed'))
        return urwid.ListBox(urwid.SimpleFocusListWalker(body))
    
    def item_chosen(self, button, choice):
        self._result = choice
        raise urwid.ExitMainLoop()
    
    
    def show(self):
        self._main = urwid.Padding(self.menu(self._message, self._options), left=2, right=2)
        top = urwid.Overlay(self._main, urwid.SolidFill(u'\N{MEDIUM SHADE}'),
            align='center', width=('relative', 60),
            valign='middle', height=('relative', 60),
            min_width=20, min_height=9)
        
        
        loop = urwid.MainLoop(top, palette=[('reversed', 'standout', '')], screen=RobotScreen())
#         loop = urwid.MainLoop(top, palette=self._pallet, screen=RobotScreen())
#         
#         loop.screen.set_terminal_properties(colors=256)
#         loop.widget = urwid.AttrMap(self._placeholder, 'bg')
#         loop.widget.original_widget = urwid.Filler(urwid.Pile([]))
        
        loop.run()
                
        return self._result;      

#Works 
class PassFailDialog(SelectionDialog):

    def __init__(self, message):
        SelectionDialog.__init__(self, message, [u'PASS', u'FAIL'])
        
    def item_chosen(self, button, choice):
        if choice == u'PASS':
            self._result = choice
        raise urwid.ExitMainLoop()
        
        
        
        
        

